<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

class KBCategoryView_list extends AppView
{
        
	var $tmpl = 'list.html';
	var $padding = 20;
	
	
	function execute(&$obj, &$manager) {
	
		$tpl = &new tplTemplatez($this->template_dir . $this->tmpl);
		
		// header generate
		$tpl->tplAssign('header', $this->commonHeaderList());
		
		// sort generate
		$sort = &$this->getSort();
		$manager->setSqlParamsOrder($sort->getSql());
		
		// get records
		$rows =& $this->stripVars($manager->getRecords());
		$tree_helper = &$manager->getTreeHelperArray($rows);
		
		// data to category
		$tpl->tplSetNeededGlobal('private');
		if($manager->isExtra('private')) {
			$roles = &$this->getRoles($rows, $manager);
		}
		
		foreach($tree_helper as $cat_id => $level) {
			
			$v = $this->getViewListVars($cat_id, $rows[$cat_id]['active'], $rows[$cat_id]['parent_id']);
			
			$v['num_entries'] = ($rows[$cat_id]['num_entries']) ? $rows[$cat_id]['num_entries'] : '';
			$v['entry_link'] = $this->getLink('knowledgebase', 'kb_entry', false, false, array('filter[c]'=>$cat_id));
			
			// private&roles
			if($manager->isExtra('private')) {
				$v['roles_msg'] = '';
				if(isset($roles[$cat_id])) { 
					$v['roles_msg'] = ' - ' . implode('<br> - ', $roles[$cat_id]);
				}
			}
				
			if($rows[$cat_id]['private']) {
				$tpl->tplSetNeeded('row/if_private');
			}
			
			if($rows[$cat_id]['commentable']) {
				$tpl->tplSetNeeded('row/if_commentable');
			}
			
			if($rows[$cat_id]['ratingable']) {
				$tpl->tplSetNeeded('row/if_ratingable');
			}                               
			
			$v['class'] = ($level == 0) ? 'trDarker' : 'trLighter';
			$v['padding'] = $this->padding*$level;
			$block = ($level == 0) ? 'level_0' : 'other_level';
			
			$tpl->tplSetNeeded('row/' . $block);
			$tpl->tplParse(array_merge($v, $rows[$cat_id], $this->msg), 'row');
		}
		
		$tpl->tplAssign($this->msg);
		$tpl->tplAssign($sort->toHtml());
		
		$tpl->tplParse();
		return $tpl->tplPrint(1);
	}
	
	
	function &getRoles($rows, $manager) {
		$ids = $manager->getValuesString($rows, 'id'); 
		$roles = ($ids) ? $this->stripVars($manager->getMemberRoleById($ids, true)) : array();
		return $roles;
	}	
	
	
	function &getSort() {
		//$sort = new TwoWaySort();
		$sort = new OneWaySort($_GET);
		$sort->default_order = 1;
		$sort->setTitleMsg('asc',  $this->msg['sort_asc_msg']);
		$sort->setTitleMsg('desc', $this->msg['sort_desc_msg']);
		$sort->setSortItem('name_msg',  'name', 'name', $this->msg['name_msg']);
		$sort->setSortItem('private_msg', 'private', 'private', $this->msg['private_msg']);
		$sort->setSortItem('status_msg','status', 'active', $this->msg['status_msg']);
		$sort->setSortItem('entries_msg','num', 'num_entries', $this->msg['entries_msg']);
		$sort->setSortItem('commentable_msg','comment', 'commentable', $this->msg['commentable_msg']);
		$sort->setSortItem('ratingable_msg','rate', 'ratingable', $this->msg['ratingable_msg']);		
		$sort->setSortItem('sort_order_msg','sort_order', 'sort_order', $this->msg['sort_order_msg'], 1);
		
		//$sort->getSql();
		//$sort->toHtml()
		return $sort;
	}
	
	
	// reassign for use with parents
	// used it to set links such as delete, update
	function getViewListVars($record_id = false, $active = NULL, $parent_id = false, $private = false) {
		
		$row = &parent::getViewListVars($record_id, $active);
		
		// insert
		$row['insert_link'] = $this->getActionLink('insert', false, array('parent_id'=>$record_id));
		$row['insert_img'] = $this->getImgLink($row['insert_link'], 'doc_icon', $this->msg['new_child_category_msg']);
		
		//delete
		$row['delete_link'] = $this->getActionLink('delete', $record_id, array('parent_id'=>$parent_id));
		$row['delete_img'] = $this->getImgLink($row['delete_link'], 'delete', $this->msg['delete_msg'], $this->msg['sure_delete_category_msg']);                
		
		return $row;
	}
}
?>
